package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.TypeOfCaseService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.TypeOfCaseDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing TypeOfCase.
 */
@RestController
@RequestMapping("/api")
public class TypeOfCaseResource {

    private final Logger log = LoggerFactory.getLogger(TypeOfCaseResource.class);

    private static final String ENTITY_NAME = "typeOfCase";

    private final TypeOfCaseService typeOfCaseService;

    public TypeOfCaseResource(TypeOfCaseService typeOfCaseService) {
        this.typeOfCaseService = typeOfCaseService;
    }

    /**
     * POST  /type-of-cases : Create a new typeOfCase.
     *
     * @param typeOfCaseDTO the typeOfCaseDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new typeOfCaseDTO, or with status 400 (Bad Request) if the typeOfCase has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/type-of-cases")
    @Timed
    public ResponseEntity<TypeOfCaseDTO> createTypeOfCase(@RequestBody TypeOfCaseDTO typeOfCaseDTO) throws URISyntaxException {
        log.debug("REST request to save TypeOfCase : {}", typeOfCaseDTO);
        if (typeOfCaseDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new typeOfCase cannot already have an ID")).body(null);
        }
        TypeOfCaseDTO result = typeOfCaseService.save(typeOfCaseDTO);
        return ResponseEntity.created(new URI("/api/type-of-cases/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /type-of-cases : Updates an existing typeOfCase.
     *
     * @param typeOfCaseDTO the typeOfCaseDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated typeOfCaseDTO,
     * or with status 400 (Bad Request) if the typeOfCaseDTO is not valid,
     * or with status 500 (Internal Server Error) if the typeOfCaseDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/type-of-cases")
    @Timed
    public ResponseEntity<TypeOfCaseDTO> updateTypeOfCase(@RequestBody TypeOfCaseDTO typeOfCaseDTO) throws URISyntaxException {
        log.debug("REST request to update TypeOfCase : {}", typeOfCaseDTO);
        if (typeOfCaseDTO.getId() == null) {
            return createTypeOfCase(typeOfCaseDTO);
        }
        TypeOfCaseDTO result = typeOfCaseService.save(typeOfCaseDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, typeOfCaseDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /type-of-cases : get all the typeOfCases.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of typeOfCases in body
     */
    @GetMapping("/type-of-cases")
    @Timed
    public ResponseEntity<List<TypeOfCaseDTO>> getAllTypeOfCases(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of TypeOfCases");
        Page<TypeOfCaseDTO> page = typeOfCaseService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/type-of-cases");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /type-of-cases/:id : get the "id" typeOfCase.
     *
     * @param id the id of the typeOfCaseDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the typeOfCaseDTO, or with status 404 (Not Found)
     */
    @GetMapping("/type-of-cases/{id}")
    @Timed
    public ResponseEntity<TypeOfCaseDTO> getTypeOfCase(@PathVariable Long id) {
        log.debug("REST request to get TypeOfCase : {}", id);
        TypeOfCaseDTO typeOfCaseDTO = typeOfCaseService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(typeOfCaseDTO));
    }

    /**
     * DELETE  /type-of-cases/:id : delete the "id" typeOfCase.
     *
     * @param id the id of the typeOfCaseDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/type-of-cases/{id}")
    @Timed
    public ResponseEntity<Void> deleteTypeOfCase(@PathVariable Long id) {
        log.debug("REST request to delete TypeOfCase : {}", id);
        typeOfCaseService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/type-of-cases?query=:query : search for the typeOfCase corresponding
     * to the query.
     *
     * @param query the query of the typeOfCase search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/type-of-cases")
    @Timed
    public ResponseEntity<List<TypeOfCaseDTO>> searchTypeOfCases(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of TypeOfCases for query {}", query);
        Page<TypeOfCaseDTO> page = typeOfCaseService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/type-of-cases");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
